#include "Common/Algebra/Transform.h"

namespace cmn {

	Transform Transform::operator += ( const Transform& tf ) {
		Vec += tf.Vec;
		Rot += tf.Rot;
		return *this;
	}

	Transform Transform::operator -= ( const Transform& tf ) {
		Vec -= tf.Vec;
		Rot -= tf.Rot;
		return *this;

	}

	Transform Transform::operator *= ( const Transform& tf ) {
		Vec = *this * tf.Vec;
		Rot += tf.Rot;
		return *this;
	}

	Transform Transform::operator *= ( const float a ) {
		Vec *= a;
		Rot *= a;
		return *this;
	}

	Matr4 Transform::GetMatrix() const {
		Matr4 matr = Matr4::Identity();

		float cosA = cos( Rot );
		float sinA = sin( Rot );

		matr( 0, 0 ) = Scale * cosA;
		matr( 0, 1 ) = Scale * sinA;
		matr( 1, 0 ) = Scale * -sinA;
		matr( 1, 1 ) = Scale * cosA;

		matr( 2, 2 ) = Scale;

		matr( 3, 0 ) = Vec.X;
		matr( 3, 1 ) = Vec.Y;
		return matr;
	}

	Matr4 Transform::GetInvMatrix() const {
		Matr4 matr = Matr4::Identity();

		float cosA = cos( Rot );
		float sinA = sin( Rot );

		matr( 0, 0 ) = cosA / Scale;
		matr( 0, 1 ) = -sinA / Scale;
		matr( 1, 0 ) = sinA / Scale;
		matr( 1, 1 ) = cosA / Scale;

		matr( 2, 2 ) = 1.0f / Scale;

		matr( 3, 0 ) = - ( Vec.X * cosA + Vec.Y * sinA ) / Scale;
		matr( 3, 1 ) = + ( Vec.X * sinA - Vec.Y * cosA ) / Scale;

		return matr;
	}
}
